#!/usr/bin/env bash
#######################################
tmoe_qemu_user_static() {
	RETURN_TO_WHERE='tmoe_qemu_user_static'
	BETA_SYSTEM=$(
		whiptail --title "qemu_user_static" --menu "QEMU的user模式跨架构运行的效率可能比system模式更高，但存在更多的局限性" 0 50 0 \
			"1" "chart架构支持表格" \
			"2" "install/upgrade(安装/更新)" \
			"3" "remove(移除/卸载)" \
			"0" "🌚 Return to previous menu 返回上级菜单" \
			3>&1 1>&2 2>&3
	)
	##############################
	case "${BETA_SYSTEM}" in
	0 | "") tmoe_qemu_user_manager ;;
	1) tmoe_qemu_user_chart ;;
	2) install_qemu_user_static ;;
	3) remove_qemu_user_static ;;
	esac
	######################
	press_enter_to_return
	tmoe_qemu_user_static
}
#####################
tmoe_qemu_user_chart() {
	cat <<-'ENDofTable'
		下表中的所有系统均支持x64和arm64
		*表示仅旧版支持
		All distributions in the table below support amd64 and arm64.
			╔═══╦════════════╦════════╦════════╦═════════╦
			║   ║Architecture║        ║        ║         ║
			║   ║----------- ║ x86    ║armhf   ║ppc64el  ║
			║   ║System      ║        ║        ║         ║
			║---║------------║--------║--------║---------║
			║ 1 ║  Debian    ║  ✓     ║    ✓   ║   ✓     ║
			║   ║            ║        ║        ║         ║
			║---║------------║--------║--------║---------║
			║   ║            ║        ║        ║         ║
			║ 2 ║  Ubuntu    ║*<=19.10║  ✓     ║   ✓     ║
			║---║------------║--------║--------║---------║
			║   ║            ║        ║        ║         ║
			║ 3 ║ Kali       ║  ✓     ║   ✓    ║    X    ║
			║---║------------║--------║--------║---------║
			║   ║            ║        ║        ║         ║
			║ 4 ║ Arch       ║  X     ║   ✓    ║   X     ║
			║---║------------║--------║--------║---------║
			║   ║            ║        ║        ║         ║
			║ 5 ║ Fedora     ║ *<=29  ║   ✓    ║  ✓      ║
			║---║------------║--------║--------║---------║
			║   ║            ║        ║        ║         ║
			║ 6 ║  Alpine    ║  ✓     ║    ✓   ║   ✓     ║
			║---║------------║--------║--------║---------║
			║   ║            ║        ║        ║         ║
			║ 7 ║ Centos     ║ *<=7   ║ *<=7   ║   ✓     ║
	ENDofTable
}
###############
check_gnu_linux_qemu_version() {
	if [ -e "${PREFIX}/bin/qemu-aarch64-static" ]; then
		LOCAL_QEMU_USER_FILE="${PREFIX}/bin/qemu-aarch64-static"
	elif [ -e "/usr/bin/qemu-aarch64-static" ]; then
		LOCAL_QEMU_USER_FILE='/usr/bin/qemu-aarch64-static'
	fi
	case ${LOCAL_QEMU_USER_FILE} in
	"") ;;
	*) LOCAL_QEMU_USER_VERSION=$(${LOCAL_QEMU_USER_FILE} --version | head -n 1 | awk '{print $5}' | cut -d ':' -f 2 | cut -d ')' -f 1) ;;
	esac
}
###########
install_qemu_user_static() {
	printf "%s\n" "正在检测版本信息..."
	LOCAL_QEMU_USER_FILE=''
	LOCAL_QEMU_USER_VERSION=''
	case ${LINUX_DISTRO} in
	Android)
		if [ -e "${QEMU_USER_LOCAL_VERSION_FILE}" ]; then
			LOCAL_QEMU_USER_VERSION=$(head -n 1 ${QEMU_USER_LOCAL_VERSION_FILE})
		fi
		;;
	*) check_gnu_linux_qemu_version ;;
	esac

	case ${LOCAL_QEMU_USER_VERSION} in
	"") LOCAL_QEMU_USER_VERSION='未安装Not Installed' ;;
	*) ;;
	esac

	cat <<-'EOF'
		---------------------------
		一般来说，新版的qemu-user会引入新的功能，并带来性能上的提升。
		尽管有可能会引入一些新bug，但是也有可能修复了旧版的bug。
		We recommend that you to use the new version.
		---------------------------
	EOF
	check_qemu_user_version
	cat <<-ENDofTable
		╔═══╦══════════╦═══════════════════╦════════════════════
		║   ║          ║                   ║                    
		║   ║ software ║    ✨最新版本     ║   本地版本 🎪
		║   ║          ║  Latest version   ║  Local version     
		║---║----------║-------------------║--------------------
		║ 1 ║qemu-user ║                    ${LOCAL_QEMU_USER_VERSION} 
		║   ║ static   ║${THE_LATEST_DEB_VERSION_CODE}

	ENDofTable
	do_you_want_to_continue
	THE_LATEST_DEB_LINK="${REPO_URL}${THE_LATEST_DEB_VERSION}"
	printf "%s\n" "${YELLOW}${THE_LATEST_DEB_LINK}${RESET}"
	printf "%s\n" "${THE_LATEST_DEB_VERSION_CODE}" >${QEMU_USER_LOCAL_VERSION_FILE}
	case "${LINUX_DISTRO}" in
	"debian")
		apt update
		printf '%s\n' 'apt install -y qemu-user-static'
		apt install -y qemu-user-static
		;;
	*) download_qemu_user ;;
	esac
}
##############
check_qemu_user_version() {
	REPO_URL='https://mirrors.bfsu.edu.cn/debian/pool/main/q/qemu/'
	THE_LATEST_DEB_VERSION="$(curl -L ${REPO_URL} | grep '\.deb' | grep 'qemu-user-static' | grep "${TRUE_ARCH_TYPE}" | tail -n 1 | cut -d '=' -f 3 | cut -d '"' -f 2)"
	THE_LATEST_DEB_VERSION_CODE=$(printf '%s\n' "${THE_LATEST_DEB_VERSION}" | cut -d '_' -f 2 | sed 's@%2B@+@')
}
###############
unxz_deb_file() {
	if [ ! $(command -v ar) ]; then
		DEPENDENCY_01='binutils'
		apt update
		printf "%s\n" "apt install -y ${DEPENDENCY_01}"
		apt install -y ${DEPENDENCY_01} || pacman -S ${DEPENDENCY_01} || dnf install ${DEPENDENCY_01} || apk add ${DEPENDENCY_01} || zypper in ${DEPENDENCY_01} || port install ${DEPENDENCY_01} || guix package -i ${DEPENDENCY_01} || pkg install ${DEPENDENCY_01} || pkg_add ${DEPENDENCY_01} || pkgutil -i ${DEPENDENCY_01} || eopkg install ${DEPENDENCY_01}
	fi
	ar xv ${THE_LATEST_DEB_VERSION}
	#tar -Jxvf data.tar.xz ./usr/bin -C $PREFIX/..
	tar -Jxvf data.tar.xz
}
########################
copy_qemu_user_bin_files() {
	cp -rf ./usr/bin ${QEMU_BIN_PREFIX}
	cd ..
	rm -rv ${TEMP_FOLDER}
}
######################
download_qemu_user() {
	if [ -z ${TMPDIR} ]; then
		TMPDIR=/tmp
		#mkdir -p ${TMPDIR}
		#chmod 777 /tmp
	fi
	cd ${TMPDIR}
	TEMP_FOLDER='.QEMU_USER_BIN'
	mkdir -p ${TEMP_FOLDER}
	cd ${TEMP_FOLDER}
	aria2c --no-conf --allow-overwrite=true -s 5 -x 5 -k 1M -o "${THE_LATEST_DEB_VERSION}" "${THE_LATEST_DEB_LINK}"
	unxz_deb_file
	copy_qemu_user_bin_files
}
##############
remove_qemu_user_static() {
	ls -lah /usr/bin/qemu-*-static ${QEMU_BIN_PREFIX}/bin/qemu-*-static 2>/dev/null
	printf "%s\n" "${RED}rm -rv${RESET} ${BLUE}${QEMU_BIN_PREFIX}/bin/qemu-*-static ${QEMU_USER_LOCAL_VERSION_FILE}${RESET}"
	printf "%s\n" "${RED}${TMOE_REMOVAL_COMMAND}${RESET} ${BLUE}qemu-user-static${RESET}"
	do_you_want_to_continue
	rm -rv ${QEMU_BIN_PREFIX}/bin/qemu-*-static "${QEMU_BIN_PREFIX}/bin/qemu-*-static" ${QEMU_USER_LOCAL_VERSION_FILE}
	${TMOE_REMOVAL_COMMAND} qemu-user-static
}
###############
qemu_user_env() {
	QEMU_USER_LOCAL_VERSION_FILE="${CONFIG_FOLDER}/qemu-user-static_version.txt"
	case ${LINUX_DISTRO} in
	Android) QEMU_BIN_PREFIX=${PREFIX} ;;
	*) QEMU_BIN_PREFIX=/usr ;;
	esac
}
##########
tmoe_qemu_user_manager() {
	cd ${CONFIG_FOLDER}
	NEW_TMOE_ARCH=''
	qemu_user_env
	RETURN_TO_WHERE='tmoe_qemu_user_manager'
	tmoe_qemu_user_manager_zh() {
		BETA_SYSTEM=$(
			whiptail --title "跨架构运行容器" --menu "您想要(模拟)运行哪个架构？\nWhich architecture do you want to simulate?" 0 50 0 \
				"0" "🌚 Return to previous menu 返回上级菜单" \
				"00" "qemu-user-static管理(跨架构模拟所需的基础依赖)" \
				"1" "x86/i386(常见于32位cpu的旧式传统pc)" \
				"2" "x64/amd64(2020年最主流的64位架构,应用于pc和服务器）" \
				"3" "arm64/aarch64(2020年移动平台主流cpu架构)" \
				"4" "armhf/armv7(32位arm架构,支持硬浮点运算)" \
				"5" "riscv64(开源架构,精简指令集)" \
				"6" "ppc64el(PowerPC,应用于通信、工控、航天国防等领域)" \
				"7" "s390x(常见于IBM大型机)" \
				"8" "mipsel(常见于嵌入式设备)" \
				"9" "mips64el(常见于龙芯cpu)" \
				"10" "armel(支持软浮点运算,常见于旧设备）" \
				3>&1 1>&2 2>&3
		)
	}
	tmoe_qemu_user_manager_ja() {
		BETA_SYSTEM=$(
			whiptail --title "Across architectures" --menu "どのアーキテクチャをシミュレートしますか？" 0 50 0 \
				"0" "🌚 戻る" \
				"00" "qemu-user-static(基本的な依存関係)" \
				"1" "i386/x86:Intel 8086、およびその後方互換性を持つマイクロプロセッサの命令セットアーキテクチャの総称" \
				"2" "x64/amd64:またはx86-64とは、x86アーキテクチャを64ビットに拡張した命令セットアーキテクチャ" \
				"3" "arm64/aarch64:64-bit extension of the ARM architecture" \
				"4" "armhf/armv7:32-bit arm hard float" \
				"5" "riscv64:確立された縮小命令セットコンピュータ の原則に基づいたオープン標準の命令セットアーキテクチャ である" \
				"6" "ppc64el:64ビットビッグエンディアンPowerPCおよびPower ISAプロセッサ用に最適化されたアプリケーションのターゲットアーキテクチャを参照するために" \
				"7" "s390x:IBMメインフレームコンピューターで一般的に見られる" \
				"8" "mipsel:MIPSアーキテクチャは、ミップス・コンピュータシステムズが開発したRISCマイクロプロセッサの命令セット・アーキテクチャ である" \
				"9" "mips64el" \
				"10" "armel:32-bit arm soft float" \
				3>&1 1>&2 2>&3
		)
	}
	tmoe_qemu_user_manager_en() {
		BETA_SYSTEM=$(
			whiptail --title "Across architectures" --menu "Which architecture do you want to simulate?" 0 50 0 \
				"0" "🌚 Return to previous menu" \
				"00" "qemu-user-static(Basic dependence)" \
				"1" "i386:x86 is a family of instruction set architectures initially developed by Intel based on the Intel 8086 microprocessor and its 8088 variant." \
				"2" "x64/amd64:It defines a 64-bit virtual address format, of which the low-order 48 bits are used in current implementations." \
				"3" "arm64:64-bit extension of the ARM architecture" \
				"4" "armhf:32-bit arm hard float" \
				"5" "riscv64:RISC-V is an open standard instruction set architecture (ISA) based on established reduced instruction set computer (RISC) principles" \
				"6" "ppc64el:a pure little-endian mode that has been introduced with the POWER8 as the prime target" \
				"7" "s390x:Linux on IBM Z is not generally appropriate on premises for small businesses that would have fewer than about 10 distributed Linux servers" \
				"8" "mipsel:Microprocessor without Interlocked Pipelined Stages little-endian" \
				"9" "mips64el" \
				"10" "armel:32-bit arm soft float" \
				3>&1 1>&2 2>&3
		)
	}
	##############################
	case ${TMOE_MENU_LANG} in
	zh_*UTF-8) tmoe_qemu_user_manager_zh ;;
	ja_JP.UTF-8) tmoe_qemu_user_manager_ja ;;
	*) tmoe_qemu_user_manager_en ;;
	esac
	############
	case "${BETA_SYSTEM}" in
	0 | "") proot_container_menu ;;
	00) tmoe_qemu_user_static ;;
	1)
		NEW_TMOE_ARCH='i386'
		case ${TRUE_ARCH_TYPE} in
		amd64 | i386) TMOE_QEMU_ARCH="" ;;
		*) TMOE_QEMU_ARCH="${NEW_TMOE_ARCH}" ;;
		esac
		;;
	2)
		NEW_TMOE_ARCH='amd64'
		TMOE_QEMU_ARCH="x86_64"
		;;
	3)
		NEW_TMOE_ARCH='arm64'
		TMOE_QEMU_ARCH="aarch64"
		;;
	4)
		NEW_TMOE_ARCH='armhf'
		case ${TRUE_ARCH_TYPE} in
		arm64 | armhf) TMOE_QEMU_ARCH="" ;;
		*) TMOE_QEMU_ARCH="arm" ;;
		esac
		;;
	5)
		NEW_TMOE_ARCH='riscv64'
		TMOE_QEMU_ARCH="riscv64"
		;;
	6)
		NEW_TMOE_ARCH='ppc64el'
		TMOE_QEMU_ARCH="ppc64le"
		;;
	7)
		NEW_TMOE_ARCH='s390x'
		TMOE_QEMU_ARCH="${NEW_TMOE_ARCH}"
		;;
	8)
		NEW_TMOE_ARCH='mipsel'
		TMOE_QEMU_ARCH="${NEW_TMOE_ARCH}"
		;;
	9)
		NEW_TMOE_ARCH='mips64el'
		TMOE_QEMU_ARCH="${NEW_TMOE_ARCH}"
		;;
	10)
		NEW_TMOE_ARCH='armel'
		case ${TRUE_ARCH_TYPE} in
		arm64 | armhf | armel) TMOE_QEMU_ARCH="" ;;
		*) TMOE_QEMU_ARCH="armeb" ;;
		esac
		;;
	esac
	######################
	if [ ! -z "${NEW_TMOE_ARCH}" ]; then
		if [ "${TRUE_ARCH_TYPE}" = "${NEW_TMOE_ARCH}" ]; then
			TMOE_QEMU_ARCH=""
		fi
		creat_tmoe_arch_file
		ARCH_TYPE=${NEW_TMOE_ARCH}

		if [ ! -e "$PREFIX/bin/qemu-x86_64-static" ] && [ ! -e "/usr/bin/qemu-x86_64-static" ]; then
			install_qemu_user_static
		fi
		case ${ARCH_TYPE} in
		mips* | riscv64) git_clone_debian_ports_distro ;;
		*) choose_which_gnu_linux_distro ;;
		esac
	fi
	press_enter_to_return
	tmoe_qemu_user_manager
}
#####################
git_clone_debian_ports_distro() {
	check_rootfs_dir
	[[ -e ${ROOTFS_DIR} ]] || mkdir -p ${ROOTFS_DIR}
	cd ${ROOTFS_DIR}
	DISTRO_NAME='debian'
	case ${ARCH_TYPE} in
	mips*) DISTRO_CODE='buster' ;;
	riscv64) DISTRO_CODE='sid' ;;
	esac
	TMOE_LINUX_CONTAINER_DISTRO="${DISTRO_NAME}-${DISTRO_CODE}"
	creat_container_edition_txt
	go_to_proot_management

	DEBIAN_ROOTFS_FILE_NAME="${DISTRO_NAME}-${DISTRO_CODE}_${ARCH_TYPE}-rootfs.tar.xz"
	if [ ! -e "${DEBIAN_ROOTFS_FILE_NAME}" ]; then
		TEMP_DIR=".DEBIAN_${ARCH_TYPE}-TEMP_FOLDER"
		[[ ! -e ${TEMP_DIR} ]] || rm -rfv ${TEMP_DIR}
		git clone --depth=1 ${AK2_GIT_URL}/${DISTRO_NAME}-${DISTRO_CODE}_${ARCH_TYPE}.git ${TEMP_DIR}
		cd ${TEMP_DIR}
		cat .container_linux_* >${DEBIAN_ROOTFS_FILE_NAME}
		mv -fv ${DEBIAN_ROOTFS_FILE_NAME} ../
		cd ..
		rm -rvf ${TEMP_DIR}
	fi
	bash -c "$(sed -n p ${TMOE_SHARE_DIR}/container/install |
		sed "s:debian-sid:debian-${DISTRO_CODE}:g")"
}
###############
#此文件靠source来执行，不包含$@
